﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Projekt
{
    public partial class newGrn : Form
    {
        private int nirNr;
        private string bonDate;
        private int shopNr;
        public newGrn(DataTable nirLines, String nr, String Date)
        {
            InitializeComponent();
            nirNr = Convert.ToInt32(nr);
            bonDate = Date;
            grn grnote = new grn();
            DataTable dt;
            Shop uzlet = new Shop();
            dt = uzlet.getShopNameList();
            shopNr = dt.Rows.Count;
            foreach (DataRow rows in dt.Rows)
            {
                produceDistribution.Columns.Add(rows[1].ToString(),rows[0].ToString());
              //  produceDistribution.Columns[rows[0].ToString()].ReadOnly = true;
            }
            //dt = grnote.getProduceList(nr.ToString());
            dt = nirLines;
            produceDistribution.DataSource = dt;
            produceDistribution.Columns["produceUM"].Visible = false;
            produceDistribution.Columns["producePrize"].Visible = false;
            produceDistribution.Columns["produceSellPrize"].Visible = false;
            produceDistribution.Columns["produceName"].ReadOnly = true;
            produceDistribution.Columns["produceQuantity"].ReadOnly = true;
        }


        /***************************************\
        |           Input Validating:           |
        |                                       |
        |-Only number allowed                   |
        |                                       |
        |                                       |
        \***************************************/

        private void nirLines_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            DataGridViewTextBoxCell cell = produceDistribution[e.ColumnIndex, e.RowIndex] as DataGridViewTextBoxCell;
            if (cell != null && e.ColumnIndex <2)
            {
                String cellValue = e.FormattedValue.ToString();
                if (cellValue != "")
                {
                    try
                    {
                        double tmp;
                        if (!double.TryParse(cellValue, out tmp))
                        {
                            //                e.Cancel = true;
                            MessageBox.Show("Only numbers allowed");
                        }
                    }
                    catch (Exception) { }//e.Cancel = true; }
                }
            }
        }

        private void grn_CellContentChanged(object sender, DataGridViewCellEventArgs e)
        {
        }


        private void newGrn_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool ok = true;
                DataTable tmp = new DataTable();
                int db = 0;
                for (int i = 0; i < produceDistribution.ColumnCount - shopNr + 1; i++)
                {
                    tmp.Columns.Add(db.ToString());
                    db++;
                }
                tmp.Columns.Add("Name");
                tmp.Columns.Add("Total");
                string[] tmprow = new string[tmp.Columns.Count];
                foreach (DataGridViewRow row in produceDistribution.Rows)
                {
                    int index = -1;
                    Double producedb = 0;
                    for (int i = 0; i < shopNr; i++)
                    {
                        if (row.Cells[i].Value == null)
                        {
                            MessageBox.Show("Input box not allowed");
                            ok = false;
                            break;
                        }
                        else { producedb += Convert.ToDouble(row.Cells[i].Value); }
                    }
                    if (producedb != Convert.ToDouble(row.Cells[shopNr + 2].Value))
                    {
                        MessageBox.Show("Invalid data");
                        ok = false;
                    }
                    if (!ok) break;
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        index++;
                        tmprow.SetValue(cell.Value.ToString(), index);
                    }
                    tmp.Rows.Add(tmprow.ToArray());
                }
                if (ok)
                {
                    grn bon = new grn();
                    bon.addGrn(tmp, nirNr.ToString(), bonDate);
                    Bill fakt = Bill.Instance;
                    fakt.writeToPDF(nirNr);
                    bon.writeToPDF(nirNr.ToString());
                    fakt = Bill.disposeInstance;
                    this.Close();
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void produceDistribution_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
